package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.query.ClientQueryObject;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("client")
public class ClientController {

    @Autowired
    private IClientService clientService;

    @RequestMapping("view")
    public String view() {
        return "client/list";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult list(ClientQueryObject qo) {
        PageResult result = clientService.query(qo);
        return result;
    }

    @RequestMapping("listAll")
    @ResponseBody
    public List<Client> listAll() {
        List<Client> clients = clientService.selectAll();
        return clients;
    }

    //通过状态查询所有正式学员
    @RequiresPermissions(value = {"client:selectAllStudentOfficial", "正式学员全选"}, logical = Logical.OR)
    @RequestMapping("selectAllStudentOfficial")
    @ResponseBody
    public List<Client> selectAllStudentOfficial() {
        List<Client> clients = clientService.selectAllStudentOfficial();
        return clientService.selectAllStudentOfficial();
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Client client) {
        JsonResult result = new JsonResult();
        try {
            if (client.getId() != null) {
                clientService.updateByPrimaryKey(client);
            } else {
                clientService.insert(client);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return result.mark("操作失败");
        }
        return result;
    }

    //放入资源池
    @RequestMapping("abandon")
    @ResponseBody
    public JsonResult abandon(Long clientId) {
        JsonResult result = new JsonResult();
        try {
            clientService.abandon(clientId);
        } catch (Exception e) {
            e.printStackTrace();
            return result.mark("操作失败");
        }
        return result;
    }

    //转正改变状态
    @RequestMapping("changeState")
    @ResponseBody
    public JsonResult changeState(Long clientId) {
        JsonResult result = new JsonResult();
        try {
            clientService.changeState(clientId);
        } catch (Exception e) {
            e.printStackTrace();
            return result.mark("操作失败");
        }
        return result;
    }

    //转正改变状态
    @RequestMapping("exportXls")
    @ResponseBody
    public void exportXls(HttpServletResponse response, ClientQueryObject qo) throws Exception {
        //设置文件下载响应头
        response.setHeader("Content-Disposition", "attachment;filename=client.xls");
        Workbook book = clientService.exportXls(qo);
        //写入数据输出到浏览器
        book.write(response.getOutputStream());
    }


}
